#include "../global.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    vtkCreate(reader, vtkDICOMImageReader);
    reader->SetDataByteOrderToLittleEndian();
    reader->SetDirectoryName(path.toStdString().c_str());
    reader->Update();

    int extent[6];
    double spacing[3];
    double origin[3];
    double center[3];

    reader->GetOutput()->GetExtent(extent);
    reader->GetOutput()->GetSpacing(spacing);
    reader->GetOutput()->GetOrigin(origin);

    center[0] = origin[0] + spacing[0] *0.5*(extent[0] + extent[1]);
    center[1] = origin[1] + spacing[1] *0.5*(extent[2] + extent[3]);
    center[2] = origin[2] + spacing[2] *0.5*(extent[4] + extent[5]);

    double axiaElements[16] = {1,0,0,0,
                               0,0.866025,-0.5,0,
                               0,0.5,0.866025,0,
                               0,0,0,1};
    vtkCreate(resliceAxes, vtkMatrix4x4);
    resliceAxes->DeepCopy(axiaElements);
    resliceAxes->SetElement(0, 3 ,center[0]);
    resliceAxes->SetElement(1, 3 ,center[1]);
    resliceAxes->SetElement(2, 3 ,center[2]);

    vtkCreate(reslice, vtkImageReslice);
    reslice->SetInputConnection(reader->GetOutputPort());
    reslice->SetOutputDimensionality(2);
    reslice->SetResliceAxes(resliceAxes);
    reslice->SetInterpolationModeToLinear();

    vtkCreate(renderer, vtkRenderer);
    vtkWidgetCreate(widget, QVTKWidget);
    QVTKWidget* vtkWidget = widget.data();
    vtkWidget->GetRenderWindow()->AddRenderer(renderer);

    vtkCreate(imageViewer, vtkImageViewer2);
    imageViewer->SetInputConnection(reslice->GetOutputPort());
    imageViewer->UpdateDisplayExtent();
    imageViewer->SetRenderWindow(vtkWidget->GetRenderWindow());
    imageViewer->SetRenderer(renderer);
    imageViewer->SetupInteractor(vtkWidget->GetRenderWindow()->GetInteractor());
    imageViewer->SetColorLevel(500);
    imageViewer->SetColorWindow(2000);

    vtkWidget->resize(WINDOW_WIDTH, WINDOW_HEIGHT);
    vtkWidget->show();

    return a.exec();
}
